package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.dto.MapObject;
import com.yami.shop.bean.model.CouponRecord;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserScoreRecord;
import com.yami.shop.service.CouponRecordService;
import com.yami.shop.service.UserScoreRecordService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Api(tags = "导出表格接口")
@RequestMapping("/output")
@RestController
public class OutputController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserScoreRecordService scoreRecordService;
    @Autowired
    private CouponRecordService couponRecordService;

    /**
     * 导出所有会员积分、优惠券情况
     */
    @ApiOperation(value = "导出所有会员积分、优惠券情况")
    @GetMapping("/all")
    public ResponseEntity all() {
        List<User> list = userService.list();
        LinkedList<MapObject> res = new LinkedList<>();
        res.add(new MapObject()
                .add("员工编号","员工编号")
                .add("积分余额","积分余额")
                .add("优惠券数量","优惠券数量"));
        for (User user : list) {
            res.add(new MapObject()
            .add("员工编号",user.getUserCode())
            .add("积分余额",user.getScore())
            .add("优惠券数量",user.getCouponCount()));
        }
        return ResponseEntity.ok(res);
    }

    /**
     * 导出一个会员的积分使用明细
     * @return
     */
    @ApiOperation(value = "导出一个会员的积分使用明细")
    @GetMapping("/score/{uid}")
    public ResponseEntity score(@PathVariable String uid) {

        LinkedList<MapObject> res = new LinkedList<>();
        User user = userService.getById(uid);
        List<UserScoreRecord> list = scoreRecordService.list(Wrappers.lambdaQuery(new UserScoreRecord())
                .eq(UserScoreRecord::getUid, uid));
        res.add(new MapObject()
                .add("用户编号","用户编号")
                .add("记录类型","记录类型")
                .add("数量","数量")
                .add("日期","日期")
                .add("备注","备注"));
        for (UserScoreRecord u : list) {
            res.add(new MapObject()
            .add("用户编号",user.getUserCode())
            .add("记录类型",u.getType()==1?"支出":"收入")
            .add("数量",u.getAmount())
            .add("日期",u.getCreateTime())
            .add("备注",u.getMark()));
        }
        return ResponseEntity.ok(res);
    }

    /**
     * 导出一个会员的优惠券使用明细
     * @return
     */
    @ApiOperation(value = "导出一个会员的优惠券使用明细")
    @GetMapping("/coupon/{uid}")
    public ResponseEntity coupon(@PathVariable String uid) {

        LinkedList<MapObject> res = new LinkedList<>();
        User user = userService.getById(uid);
        List<CouponRecord> list = couponRecordService.list(Wrappers.lambdaQuery(new CouponRecord())
                .eq(CouponRecord::getUid, uid));
        res.add(new MapObject()
                .add("用户编号","用户编号")
                .add("类型","类型")
                .add("数量","数量")
                .add("日期","日期")
                .add("备注","备注"));
        for (CouponRecord c : list) {
            res.add(new MapObject()
            .add("用户编号",user.getUserCode())
            .add("类型",c.getType()==1?"发放":"使用")
            .add("数量",c.getCount())
            .add("日期",c.getCreateTime())
            .add("备注",c.getMark()));
        }
        return ResponseEntity.ok(res);
    }

}
